package com.tuju.currency.network.net;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import static com.tuju.currency.network.DataService.API_BASE;
import static com.tuju.currency.network.DataService.OSS_BASE;
import static com.tuju.currency.network.DataService.WECHAT_API;

public class BuildFactory {
    private static volatile BuildFactory instance;
    private Gson gson;
    private Object baseHttps;
    private Object ossHttps;
    private Object wechatHttps;

    private static final int TIME_OUT = 30;//超时时间

    public static BuildFactory getInstance() {
        if (instance == null) {
            synchronized (BuildFactory.class) {
                if (instance == null) {
                    instance = new BuildFactory();
                }
            }
        }
        return instance;
    }

    public <T> T create(Class<T> a, String type) {
        switch (type) {
            case API_BASE:
                if (baseHttps == null) {
                    synchronized (BuildFactory.class) {
                        if (baseHttps == null) {
                            baseHttps = getBuilder(type).build().create(a);
                        }
                    }
                }
                return (T) baseHttps;
            case OSS_BASE:
                if (ossHttps == null) {
                    synchronized (BuildFactory.class) {
                        if (ossHttps == null) {
                            ossHttps = getBuilder(type).build().create(a);
                        }
                    }
                }
                return (T) ossHttps;
            case WECHAT_API:
                if (wechatHttps == null) {
                    synchronized (BuildFactory.class) {
                        if (wechatHttps == null) {
                            wechatHttps = getBuilder(type).build().create(a);
                        }
                    }
                }
                return (T) wechatHttps;
            default:
                if (baseHttps == null) {
                    synchronized (BuildFactory.class) {
                        if (baseHttps == null) {
                            baseHttps = getBuilder(type).build().create(a);
                        }
                    }
                }
                return (T) baseHttps;
        }
    }

    private Retrofit.Builder getBuilder(String apiUrl) {
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.client(getOkClient());
        builder.baseUrl(apiUrl);
        builder.addConverterFactory(new NullOnEmptyConverterFactory());
//        builder.addConverterFactory(GsonConverterFactory.create(getGson()));
        builder.addConverterFactory(GsonConverterFactory.create());
        builder.addConverterFactory(ScalarsConverterFactory.create());
        builder.addCallAdapterFactory(RxJavaCallAdapterFactory.create());
        return builder;
    }

    private Gson getGson() {
        if (gson == null) {
            GsonBuilder builder = new GsonBuilder();
            builder.setLenient();
//            builder.setFieldNamingStrategy(new AnnotateNaming());
            builder.serializeNulls();
            gson = builder.create();
        }
        return gson;
    }

    private OkHttpClient getOkClient() {
        OkHttpClient client;
        client = getUnsafeOkHttpClient();
        return client;
    }

    private OkHttpClient getUnsafeOkHttpClient() {
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
        okBuilder.readTimeout(TIME_OUT, TimeUnit.SECONDS);
        okBuilder.connectTimeout(TIME_OUT, TimeUnit.SECONDS);
        okBuilder.writeTimeout(TIME_OUT, TimeUnit.SECONDS);
        okBuilder.addInterceptor(getInterceptor());
        return okBuilder.build();
    }

    //日志拦截器
    private HttpLoggingInterceptor getInterceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); // 测试
        return interceptor;
    }
}
